﻿Imports System.Globalization
Partial Class WebForm_ManageFinancePercentageFinance_FinancePercentageAuth
    Inherits System.Web.UI.UserControl
#Region "事件"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack() Then
            Me.BindDDLDivision()
            Me.BindDDLSignature()
        End If

    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Me.MonthCode = Me.VersionControl1.MonthCode
        Me.Signature = Me.ddlSignature.SelectedValue

        Me.BindGV()
        Me.plList.Visible = True
    End Sub
    Protected Sub ddlDivision_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDivision.SelectedIndexChanged
        Me.BindDDLSignature()
    End Sub
    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            '停掉ByMonth 功能 Add By Angus 20110830
            '    Dim chkSelect As CheckBox = TryCast(e.Row.FindControl("chkSelect"), CheckBox)
            '    Dim chkSelectByMonth As CheckBox = TryCast(e.Row.FindControl("chkSelectByMonth"), CheckBox)

            '    chkSelect.Attributes("onclick") = String.Format("CheckAnother('{0}', this.checked);", chkSelectByMonth.ClientID)
            '    chkSelectByMonth.Attributes("onclick") = String.Format("CheckAnother('{0}', this.checked);", chkSelect.ClientID)
        End If
    End Sub

    Protected Sub btnApply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        Dim MonthCodeList As New List(Of String)
        MonthCodeList.Add(Me.MonthCode)

        '備份下個月
        If Me.hideSaveToNext.Value = "Y" AndAlso Me.hideSpecialMonth.Value = "N" Then
            Dim DateTimeList As String() = {"yyyyMM"}
            Dim dt As DateTime = DateTime.ParseExact(Me.MonthCode, DateTimeList, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces)
            MonthCodeList.Add(dt.AddMonths(1).ToString("yyyyMM"))
        End If

        sql.Length = 0
        For k As Integer = 0 To MonthCodeList.Count - 1
            For i As Integer = 0 To Me.gv.Rows.Count - 1
                Dim cb As CheckBox = DirectCast(Me.gv.Rows(i).FindControl("chkSelect"), CheckBox)
                Dim cbAuth As CheckBox = DirectCast(Me.gv.Rows(i).FindControl("chkSelectAuth"), CheckBox)
                If cb.Checked = True OrElse cbAuth.Checked = True Then
                    sql.AppendFormat("Delete From TCReportFixed ")
                    sql.AppendFormat("Where 1=1 ")
                    sql.AppendFormat("And YYMM ='{0}' ", MonthCodeList(k))
                    sql.AppendFormat("And Signature ='{0}' ", Me.Signature)
                    sql.AppendFormat("And ItemName='{0}'; ", DirectCast(Me.gv.Rows(i).FindControl("lblItemName"), Label).Text)
                    sql.AppendFormat("Insert Into TCReportFixed (YYMM,Signature,ItemName,Fixed,Auth) ")
                    sql.AppendFormat("Select ")
                    sql.AppendFormat("'{0}' ", MonthCodeList(k))
                    sql.AppendFormat(",'{0}' ", Me.Signature)
                    sql.AppendFormat(",'{0}' ", DirectCast(Me.gv.Rows(i).FindControl("lblItemName"), Label).Text)
                    If cb.Checked = True Then
                        sql.AppendFormat(",'Y' ")
                    Else
                        sql.AppendFormat(",'N' ")
                    End If
                    If cbAuth.Checked = True Then
                        sql.AppendFormat(",'Y'; ")
                    Else
                        sql.AppendFormat(",'N'; ")
                    End If
                Else
                    sql.AppendFormat("Delete From TCReportFixed ")
                    sql.AppendFormat("Where 1=1 ")
                    sql.AppendFormat("And YYMM ='{0}' ", MonthCodeList(k))
                    sql.AppendFormat("And Signature ='{0}' ", Me.Signature)
                    sql.AppendFormat("And ItemName='{0}' ;", DirectCast(Me.gv.Rows(i).FindControl("lblItemName"), Label).Text)
                End If
            Next
        Next

        Try
            dat.AccData(sql)
            MyObj.ShowMessage("Apply Successful!")
        Catch ex As Exception
            MyObj.WriteLog(sql.ToString())
            MyObj.ShowMessage("Apply Failed!")
        End Try
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.BindGV()
    End Sub
#End Region

#Region "繫結資料"
    Private Sub BindDDLDivision()
        Dim ddlControl As DropDownList = Me.ddlDivision
        Dim dvManager As New DivisionManager

        ddlControl.DataTextField = "Division"
        ddlControl.DataValueField = "Division"
        ddlControl.DataSource = dvManager.GetAllDivision()
        ddlControl.DataBind()
    End Sub
    Private Sub BindDDLSignature()
        Dim ddlControl As DropDownList = Me.ddlSignature
        Dim stManager As New SignatureManager

        ddlControl.DataTextField = "Description"
        ddlControl.DataValueField = "Signature"
        ddlControl.DataSource = stManager.GetSignatureNoNewCategories(Me.ddlDivision.SelectedValue)
        ddlControl.DataBind()
    End Sub
    Public Sub BindGV()


        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0
        sql.AppendFormat("SELECT  distinct cv.Division,p.Signature_Text as SignatureText,cm.Text as ItemName,cm.Sequence ")
        sql.AppendFormat(",Case When cm.Configurable ='N' THEN '0' ELSE '1' END as IsEnable ")
        sql.AppendFormat(",Case When cm.Configurable ='N' THEN '1' ELSE '0' END as IsBold  ")
        sql.AppendFormat(",Case When isnull(tc.Fixed,'N') ='Y' then '1'  else '0' end as IsCheck ")
        sql.AppendFormat(",Case When isnull(tc.Auth,'N') ='Y' then '1'  else '0' end as IsCheckAuth ")
        sql.AppendFormat("FROM ChannelView cv ")
        sql.AppendFormat("INNER JOIN (SELECT Text,Sequence,Configurable FROM CodeMapping ")
        sql.AppendFormat("WHERE TableName ='TCReport' AND ColumnName='FixedPercentage') cm ON 1=1 ")
        sql.AppendFormat("INNER JOIN ProductHierarchy p ON cv.Signature=p.Signature ")
        sql.AppendFormat("LEFT JOIN TCReportFixed tc on cv.Signature=tc.Signature and cm.Text=tc.ItemName ")
        sql.AppendFormat("AND tc.YYMM='{0}' ", Me.VersionControl1.MonthCode)
        sql.AppendFormat("WHERE cv.Division='{0}' AND cv.Signature='{1}' ", ddlDivision.SelectedValue, ddlSignature.SelectedValue)
        sql.AppendFormat("ORDER BY cv.Division ,cm.Sequence ")

        gv.DataSource = dat.GetData(sql)
        gv.DataBind()

    End Sub
#End Region

#Region "屬性"

    Public Property MonthCode() As String
        Get
            Return ViewState("MonthCode")
        End Get
        Set(ByVal value As String)
            If Right(value, 1) = "B" OrElse Right(value, 1) = "T" Then
                Me.hideSpecialMonth.Value = "Y"
            Else
                Me.hideSpecialMonth.Value = "N"
            End If
            ViewState("MonthCode") = value
        End Set
    End Property

    Public Property Signature() As String
        Get
            Return ViewState("Signature")
        End Get
        Set(ByVal value As String)
            ViewState("Signature") = value
        End Set
    End Property
#End Region

End Class
